package com.ruoyi.hr.service.impl.salaryadmin.salaryopen;


import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.salaryadmin.SalaryAdminMapper;
import com.ruoyi.hr.mapper.salaryadmin.salaryopen.SalaryOpenMapper;
import com.ruoyi.hr.service.salaryadmin.salaryopen.SalaryOpenService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SalaryOpenServiceImpl implements SalaryOpenService {

    @Autowired
    SalaryOpenMapper salaryOpenMapper;

    @Autowired
    SalaryAdminMapper salaryAdminMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map, List<Authority> authorityList, int isAdmin) {
        List<Map<String, Object>> mapList = salaryAdminMapper.findProObject(map.get("pgid").toString());
        StringBuffer sql = new StringBuffer();
        for (Map map1 : mapList) {
            sql.append("sum (case 项目 when N'" + map1.get("title").toString() + "' then 值 else null end)" + map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString() + ",");
        }
        List<Map<String, Object>> mapList1 = salaryOpenMapper.findData(map, sql.toString(), authorityList, isAdmin);

        for (Map map1 : mapList) {
            //循环薪资项目
            String SalaryKey = map1.get("title").toString().replace("（", "").replace("）", "") + "@@@" + map1.get("id").toString();
            for (Map map2 : mapList1) {
                //循环人
                if (!map2.containsKey(SalaryKey)) {
                    map2.put(SalaryKey, 0);
                }
            }
        }
       return mapList1;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findDataHistory(Map map, List<Authority> authorityList, int isAdmin) {
        List<String> proList = salaryAdminMapper.findProByPgid(map.get("pgid").toString());
        map.put("pro", proList);
        return salaryOpenMapper.findDataHistory(map, authorityList, isAdmin);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String procedureRegister(String ids, String procedure) {
        String[] idArray = ids.split(",");
        String sql;//存储过程语句
        String msg;//异常信息
        //叠加异常信息
        String msgAdd = "";
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
        String urid = maps.get("id").toString();

        for (String id : idArray) {
            sql = "declare @RetVal varchar(50) exec " + procedure + " " + id + "," + urid + ",@RetVal output select @RetVal";
            String retVal = salaryOpenMapper.executeProcedure(sql);
            msg = skyMsgAlarmMapper.getMsgById(retVal);
            if (msg != null) {
                msgAdd += salaryOpenMapper.findNameByPeid(id) + ":" + msg + "\n";
            }
        }
        return msgAdd;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateSalary(Map map) {
        //先查有没有这个项目
        if(salaryOpenMapper.countSalary(map) > 0){
            return salaryOpenMapper.updateSalary(map);
        }else{
            return salaryOpenMapper.insertSalary(map);
        }
    }
}
